//
//  LDTRadioDetailsController.m
//  EPA
//
//  Created by lanou3g on 16/4/22.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "LDTRadioDetailsController.h"
#import "LDTRadioDatailsModel.h"
#import "LDTRadioDetailsCell.h"
#import "LDTNetWorkRequestManager.h"
#import "AWRadioModel.h"
#import "LDTRedioPlayController.h"
#import "LORefresh.h"
@interface LDTRadioDetailsController ()
///数据源数组
@property(nonatomic,strong)NSMutableArray *radioArray;
///请求内容起始条数
@property(nonatomic,strong)NSString *start;
//hader属性
@property(nonatomic,strong)LORefresh *headerRefresh;
@end

@implementation LDTRadioDetailsController

//移除观察者,防止返回的时候崩溃
- (void)dealloc
{
    [self.tableView removeObserver:[self.tableView defaultHeader]  forKeyPath:@"contentOffset"];  //gif图
    
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.title = @"电台详情";
    self.radioArray = [NSMutableArray new];
    
    //创建背景View
    UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    backView.image = [UIImage imageNamed:@"毛玻璃"];
    self.tableView.backgroundView = backView;
    //毛玻璃
    UIBlurEffect *abEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:abEffect];
    effectView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [backView addSubview:effectView];

    
    
    //请求数据
    self.start = @"0";
    [self loadDataFromServerRadioid:self.radioModel.radioid Start:self.start];
    
    //下拉刷新
    __weak typeof(self)weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:(LORefreshViewTypeHeaderDefault) refreshingBlock:^{
        [weakSelf.radioArray removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf loadDataFromServerRadioid:weakSelf.radioModel.radioid Start:weakSelf.start] ;
    }];
    self.headerRefresh = [self.tableView defaultHeader];
    
    //添加上拉加载
    [self.tableView addRefreshWithRefreshViewType:(LORefreshViewTypeFooterDefault) refreshingBlock:^{
        NSInteger count = weakSelf.radioArray.count;
        weakSelf.start = [NSString stringWithFormat:@"%ld",(long)count];
        [weakSelf loadDataFromServerRadioid:weakSelf.radioModel.radioid Start:weakSelf.start];
    }];
    

    [self.tableView registerNib:[UINib nibWithNibName:@"LDTRadioDetailsCell" bundle:nil] forCellReuseIdentifier:@"cell"];

}
//请求数据
- (void)loadDataFromServerRadioid:(NSString *)radioid Start:(NSString *)start
{
    [LDTNetWorkRequestManager requestURL:[NSURL URLWithString:RADIODETAILLIST_URL] Method:LDT_POST Params:@{@"radioid":self.radioModel.radioid, @"auth":@"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        // NSLog(@"%@",jsonDict);
        NSDictionary *dataDict = jsonDict[@"data"][@"list"];
        for (NSDictionary *itemDict in dataDict) {
            LDTRadioDatailsModel *rModel  =[LDTRadioDatailsModel new];
            [rModel setValuesForKeysWithDictionary:itemDict];
            [self.radioArray addObject:rModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [[self.tableView defaultHeader]endRefreshing];
            [[self.tableView defaultFooter]endRefreshing];
        });
    }];
}

#pragma mark - 点击跳转至电台播放页 LDTRedioPlayController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDTRedioPlayController *playVC = [[LDTRedioPlayController alloc]init];
    
    
    playVC.radioArray = self.radioArray;
    playVC.currentIndex  = indexPath.row;
    
    [self.navigationController pushViewController:playVC animated:YES];

}




#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return  self.radioArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    LDTRadioDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = self.radioArray[indexPath.row];

    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
